﻿using ArtMath.Attribute;
using ArtMath.Component;
using ArtMath.Component.Line;
using ArtMath.Component.Point;
using ArtMath.Resource;
using System;

namespace ArtMath.Parameter
{
    /// <summary>
    /// 点到直线的距离
    /// </summary>
    [Serializable]
    public class PointStraightLineDistanceParameter : CalculatedParameter
    {
        PointBase pt;
        StraightLineBase line;
        public PointStraightLineDistanceParameter(SketchPad pad, string name, PointBase pt, StraightLineBase line) : base(pad, name, ToolTypeName.ddzxdjl)
        {
            Point = pt;
            StraightLine = line;
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("Point")]
        public PointBase Point
        {
            get { return pt; }
            set
            {
                if (pt is object)
                {
                    pt.CalculatedParameters.Remove(this);
                }
                pt = value;
                if (pt is object)
                {
                    pt.CalculatedParameters.Add(this);
                    Value.SetExpression(GetNewValue());
                }
            }
        }
        [MultilingualCategory("Structure"), MultilingualDisplayName("StraightLine")]
        public StraightLineBase StraightLine
        {
            get { return line; }
            set
            {
                if (line is object)
                {
                    line.CalculatedParameters.Remove(this);
                }
                line = value;
                if (line is object)
                {
                    line.CalculatedParameters.Add(this);
                    Value.SetExpression(GetNewValue());
                }
            }
        }
        public override double GetNewValue()
        {
            if (Point is null || StraightLine is null)
            {
                return 0;
            }
            return Point.NewXY.DistanceTo(StraightLine.NewA, StraightLine.NewB, StraightLine.NewC);
        }
        public override void Delete(bool recoverable = false)
        {
            base.Delete(recoverable);
            if (recoverable)
            {

            }
            else
            {
                Point = null;
                StraightLine = null;
            }
        }
        protected override void ForEachRelatedObject(Predicate<Base> action)
        {
            if (action == null) return;
            if (!action(Point)) return;
            if (!action(StraightLine)) return;
        }
        public override void ChangePoint(PointBase oldPoint, PointBase newPoint)
        {
            if (ReferenceEquals(Point, oldPoint))
            {
                Point = newPoint;
            }
            else
            {
                base.ChangePoint(oldPoint, newPoint);
            }
        }
    }
}
